package offer;

import java.util.LinkedList;
import java.util.Queue;

public class MaxDepth_55 {
    private int res;
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return left > right ? left + 1 : right + 1;

    }
    public int maxDepth2(TreeNode root) {
        if(root == null) return 0;
        int left = maxDepth2(root.left);
        int right = maxDepth2(root.right);
        return left > right ? left + 1 : right + 1;
    }

    public int maxDepth3(TreeNode root) {
        if(root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int res = 0;
        while (!queue.isEmpty()){
            res++;
            int n = queue.size();
            for (int i = 0; i < n; i++) {
                TreeNode poll = queue.poll();
                if(poll.left != null) queue.add(poll.left);
                if(poll.right != null) queue.add(poll.right);
            }
        }
        return res;

    }

  public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

}
